package com.example.demo.controller;

import com.example.demo.common.Constant;
import com.example.demo.common.SessionUtil;
import com.example.demo.entity.ArtInfo;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.ArtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author CY
 * @date 2023/03/14 16:13
 **/
@RestController
@RequestMapping("/art")
public class ArtController {
    @Autowired
    private ArtService artService;

    @RequestMapping("/insert")
    public Integer insert(String title, String content, HttpServletRequest request) {
        if (!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return null;
        }
        final UserInfo session = SessionUtil.getSession(request);
        //插入
        return artService.insert(session.getId(), title, content);
    }


    @RequestMapping("/my-list")
    public List<ArtInfo> getMyArt(HttpServletRequest request, HttpServletResponse response) {
        final HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(Constant.SESSION_USERINFO_KEY) != null) {
            //根据Session获取用户id;
            UserInfo userInfo = (UserInfo) session.getAttribute(Constant.SESSION_USERINFO_KEY);
            //调用ArtService 进行查询
            return artService.getMyArt(userInfo.getId());
        }
        response.setStatus(401);
        return null;
    }

    @RequestMapping("/detailbyid")
    public ArtInfo getArtById(Integer aid) {
        //效验
        if (aid == null || aid <= 0) {
            return null;
        }
        //去数据库查询
        final ArtInfo artById = artService.getArtById(aid);
        if (artById != null && artById.getId().equals(aid)) {
            return artById;

        }
        return null;
    }

    @RequestMapping("/update")
    public Integer updateArt(Integer aid, String title, String content, HttpServletRequest request) {
        //参数效验
        if (aid < 0 || !StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return -1;
        }
        //获取当前session信息
        final UserInfo userInfo = SessionUtil.getSession(request);
        if (userInfo == null) {
            return -1;
        }
        //判断用户合法信息
        final ArtInfo artById = artService.getArtById(aid);
        if (artById == null || !Objects.equals(artById.getUid(), userInfo.getId())) {
            return -1;
        }
        //执行更新操作
        return artService.updateArt(aid, artById.getUid(), title, content);
    }

    @RequestMapping("/get-detail")
    public ArtInfo getDetail(Integer id) {
        //参数效验
        if (id == null || id < 0) {
            return null;
        }
        //查询数据库
        final ArtInfo artById = artService.getArtById(id);
        if (artById != null && artById.getId() > 0) {
            return artById;
        }
        return null;
    }

    @RequestMapping("/delete")
    public Integer delete(Integer id, HttpServletRequest request) {
        //参数效验
        if (id == null || id < 0) {
            return -1;
        }
        //获取当前用户
        final UserInfo userInfo = SessionUtil.getSession(request);
        //查询当前博客信心
        final ArtInfo artById = artService.getArtById(id);
        //判断博客中的信息是否与session一致
        if (Objects.equals(artById.getUid(), userInfo.getId())) {
            //删除
            return artService.delete(userInfo.getId(), id);
        }
        return -1;
    }

    @RequestMapping("/list")
    public List<ArtInfo> getAllList(Integer pindex, Integer psize) {
        //参数校验
        if (pindex == null || psize == null) {
            return null;
        }
        //封装参数
        int offset = (pindex - 1) * psize;
        return artService.getAllList(psize, offset);
    }

    @RequestMapping("/totalpage")
    public Integer getPages(Integer psize) {
        if (psize == null || psize <= 0) {
            return null;
        }
        //插混数据库有多少条数据;
        final Integer count = artService.getPages(psize);

        if (count <= 0) {
            return -1;
        }
        if(Objects.equals(Math.round(count*1.0/psize),count/psize)){
            return count/psize;
        }else{
            return count/psize+1;
        }
    }

}
